﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Sys.Common
{
    /// <summary>
    /// Http处理
    /// </summary>
    public static class HttpHelper
    {
        /// <summary>
        /// Get请求制定地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="cookies">Cookie</param>
        /// <returns>结果HTML内容</returns>
        public static string GetPageHtml(string url, CookieContainer cookies)
        {
            HttpContext context = HttpContext.Current;

            string html = string.Empty;

            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                request.CookieContainer = cookies;
                request.UserAgent = context.Request.UserAgent;
                request.ContentType = context.Request.ContentType;
                request.Method = "GET";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                StreamReader sr = new StreamReader(response.GetResponseStream());

                html = sr.ReadToEnd(); request.Abort(); sr.Close(); response.Close();
            }
            catch (Exception ex) { throw ex; }

            return html;
        }
    }
}
